$(function () {
    var daywork_tab = $('#daywork_tab');
    var daywork_edit = $("#daywork_edit");
    var daywork_dialog = $("#daywork_dialog");
    var edaywork_edit = $("#edaywork_edit");
    var edaywork_dialog = $("#edaywork_dialog");
    daywork_tab.datagrid({
        url:'/daywork/list',
        fit:true,
        fitColumns:true,
        border:false,
        rownumbers:true,
        pagination:true,
        singleSelect:true,
        toolbar:'#daywork_tab_tool',
        columns:[[
            {field:'id',title:'任务编号',align:'center',width:50},
            {field:'workTime',title:'时间',align:'center',width:100},
            {field:'employee',title:'处理人',align:'center',width:100,formatter:formatterEmployee},
            {field:'workDescriptionString',title:'任务描述',align:'center',width:100},
            {field:'descriptionHandleString',title:'处理描述',align:'center'},
            {field:'stateString',title:'状态',align:'center',width:100}
        ]]
    });
    //格式化信息
    function formatterEmployee(value,row,index) {
        return value ? value.realName:"";
    }

    var objMethod = {
        //刷新页面
        reloadList:function () {
            daywork_tab.datagrid("reload");
        },
        savework:function () {
            var url = "/daywork/save";
            //获取dayworkid
            var dayworkId = $("#daywork_id").val();
            if (dayworkId){
                url = "/daywork/update"
            }
            //提交表单
            daywork_edit.form("submit",{
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },

        //打开编辑窗口
        adddaywork:function () {
            //清空表中数据
            daywork_edit.form("clear");
            daywork_dialog.dialog("setTitle","编辑");
            daywork_dialog.dialog("open");
        },
        //daywork  delete
        deletedaywork:function () {
            //获取选中的行
            var selectedRow = daywork_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定删除",function (yes) {
               if (yes){
                   $.post("/daywork/delete",{id:selectedRow.id},function (data) {
                      if(data.success){
                          $.messager.alert("提示","操作成功","info",function () {
                              //刷新页面
                              objMethod.reloadList();
                          });
                      } else {
                          $.messager.alert("提示",data.msg,"error");
                      }
                   });
               }
            });
        },
        //daywork编辑
        editdaywork:function () {
            //获取选中的行
            var selectedRow = daywork_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //设置表单回显--清空表中数据
            daywork_edit.form("clear");

            if (selectedRow.employee){
                //alert(selectedRow.employee.id);
                selectedRow['employee.id'] = selectedRow.employee.id;
            }

            daywork_edit.form("load",selectedRow);
            daywork_dialog.dialog("setTitle","编辑");
            daywork_dialog.dialog("open");
        },
        esavework:function () {
            var url = "/daywork/esave";
            //获取dayworkid
            var dayworkId = $("#daywork_id").val();
            //提交表单
            edaywork_edit.form("submit",{
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },

        //daywork编辑
        updatework:function () {
            //获取选中的行
            var selectedRow = daywork_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //设置表单回显--清空表中数据
            edaywork_edit.form("clear");

            edaywork_edit.form("load",selectedRow);
            edaywork_dialog.dialog("setTitle","编辑");
            edaywork_dialog.dialog("open");
        },


        //标记任务失败
        workfail:function () {
            //获取选中的行
            var selectedRow = daywork_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定任务失败",function (yes) {
                if (yes){
                    $.post("/daywork/fail",{id:selectedRow.id},function (data) {
                        if(data.success){
                            $.messager.alert("提示","操作成功","info",function () {
                                //刷新页面
                                objMethod.reloadList();
                            });
                        } else {
                            $.messager.alert("提示",data.msg,"error");
                        }
                    });
                }
            });
        },
        //标记任务完成
        worksuccess:function () {
            //获取选中的行
            var selectedRow = daywork_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定任务成功",function (yes) {
                if (yes){
                    $.post("/daywork/success",{id:selectedRow.id},function (data) {
                        if(data.success){
                            $.messager.alert("提示","操作成功","info",function () {
                                //刷新页面
                                objMethod.reloadList();
                            });
                        } else {
                            $.messager.alert("提示",data.msg,"error");
                        }
                    });
                }
            });
        },




        //关闭窗口
        cancel:function () {
            daywork_dialog.dialog("close");
            edaywork_dialog.dialog("close");
        },
        //高级查询
        queryForList:function () {
            var keyword = $("#keyword").val();
            var workTime = $("#workTime").val();
            daywork_tab.datagrid("load",{keyword:keyword,workTime:workTime});
        }/*,
        downloadList:function () {
            window.location.href="/daywork/download";
        }*/

    };
    //统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        objMethod[cmd]();
    });
    //新增daywork弹框
    daywork_dialog.dialog({
        title: '编辑',
        closed: true,
        buttons:'#daywork_edit_tool'
    });
    //新增daywork描述处理弹框
    edaywork_dialog.dialog({
        title: '编辑',
        closed: true,
        buttons:'#edaywork_edit_tool'
    });
});
